﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateFlowStepDesign;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateFlowStepDesign
{
	/// <summary>
	/// 工作流模板流程步骤设计
	/// </summary>
	public class WorkflowTemplateFlowStepDesignData : IWorkflowTemplateFlowStepDesignData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplateflowstepdesign";
		private const string QUERY = @"SELECT WorkflowTemplateFlowStepDesignId,EditAdminName,TemplateCode,TemplateName,TotalSteps,CreateTime,CreateAdminId,CreateAdminName,UpdateTime,EditAdminId FROM oa_workflowtemplateflowstepdesign";
		public WorkflowTemplateFlowStepDesignData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板流程步骤设计是否存在
		public async Task<bool> Exists(long workflowTemplateFlowStepDesignId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateFlowStepDesignId=?WorkflowTemplateFlowStepDesignId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFlowStepDesignId", workflowTemplateFlowStepDesignId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板流程步骤设计
		public async Task<WorkflowTemplateFlowStepDesignEntity> Add(WorkflowTemplateFlowStepDesignEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplateflowstepdesign", new string[] { "TemplateCode", "TemplateName", "TotalSteps", "CreateAdminId", "CreateAdminName", });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateFlowStepDesignId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TotalSteps", entity.TotalSteps, DbType.Int32, size: 11);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateFlowStepDesignEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流模板流程步骤设计
		public async Task<WorkflowTemplateFlowStepDesignEntity> Update(WorkflowTemplateFlowStepDesignEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplateflowstepdesign", new string[] { "EditAdminName", "TemplateCode", "TemplateName", "TotalSteps", "EditAdminId" }, new string[] { "WorkflowTemplateFlowStepDesignId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateFlowStepDesignId=?WorkflowTemplateFlowStepDesignId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFlowStepDesignId", entity.WorkflowTemplateFlowStepDesignId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TotalSteps", entity.TotalSteps, DbType.Int32, size: 11);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateFlowStepDesignEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板流程步骤设计
		public async Task<bool> Delete(long workflowTemplateFlowStepDesignId)
		{
			string sql = @"DELETE FROM oa_workflowtemplateflowstepdesign WHERE WorkflowTemplateFlowStepDesignId=?WorkflowTemplateFlowStepDesignId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFlowStepDesignId", workflowTemplateFlowStepDesignId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		public async Task<bool> Delete(string templateCode)
		{
			string sql = @"DELETE FROM oa_workflowtemplateflowstepdesign WHERE TemplateCode=?TemplateCode";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString,size:50);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板流程步骤设计
		public async Task<WorkflowTemplateFlowStepDesignEntity> GetEntity(long workflowTemplateFlowStepDesignId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateFlowStepDesignId=?WorkflowTemplateFlowStepDesignId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateFlowStepDesignId", workflowTemplateFlowStepDesignId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateFlowStepDesignEntity>(sql, parameters);
		}
		public async Task<WorkflowTemplateFlowStepDesignEntity> GetEntity(string templateCode)
		{
			string sql = string.Format(@"{0} WHERE TemplateCode=?TemplateCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?TemplateCode", templateCode, DbType.AnsiString, size: 50);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateFlowStepDesignEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板流程步骤设计分页
		public async Task<(IEnumerable<WorkflowTemplateFlowStepDesignEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplateflowstepdesign {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateFlowStepDesignEntity, int>(sql, parameters);
		}
        #endregion
    }
}



